package org.springblade.modules.admin.service.erp.impl;

import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springblade.core.log.exception.ServiceException;
import org.springblade.core.tool.utils.Func;
import org.springblade.modules.admin.dto.erp.PartnerCloudTableItemDTO;
import org.springblade.modules.admin.dto.erp.PartnerCloudTableSaveOrUpdateDTO;
import org.springblade.modules.admin.enums.erp.ERPTableEnum;
import org.springblade.modules.admin.enums.erp.options.BuildingMessageTypeEnum;
import org.springblade.modules.admin.enums.erp.tables.BuildingMessageEnum;
import org.springblade.modules.admin.service.BuildingService;
import org.springblade.modules.admin.service.erp.IPartnerCloudBuildingService;
import org.springblade.modules.admin.service.erp.IPartnerCloudSpaceTableService;
import org.springblade.modules.admin.vo.erp.service.BuildingMessageVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: Sharry
 * @createTime: 2024-12-26 15:28
 * @version: Version-1.0
 */
@Slf4j
@Service
public class PartnerCloudBuildingServiceImpl implements IPartnerCloudBuildingService {

	private IPartnerCloudSpaceTableService partnerTableService;

	@Autowired
	public void setPartnerTableService(IPartnerCloudSpaceTableService partnerTableService) {
		this.partnerTableService = partnerTableService;
	}

	private BuildingService buildingService;

	@Autowired
	@Lazy
	public void setBuildingService(BuildingService buildingService) {
		this.buildingService = buildingService;
	}

	@Override
	public void syncErpBuilding(Long buildingId) {
		if(isProdEnv()){

		}
		List<BuildingMessageVO> buildingMessageVOList = buildingService.selectBuildingMessageListByBuildingId(buildingId);
		if(Func.isNotEmpty(buildingMessageVOList)){
			asyncPurchase(buildingMessageVOList);
		}
	}

	private boolean isProdEnv(){
		String prodEnv = "prod";
		String activeProfile = System.getProperty("spring.profiles.active");
		return prodEnv.equals(activeProfile);
	}

	@Async
	protected void asyncPurchase(List<BuildingMessageVO> buildingMessageVOList){
		String tableId = ERPTableEnum.BUILDING_MESSAGE.getTableId();
		// 2. 处理数据/数据封装
		PartnerCloudTableSaveOrUpdateDTO saveOrUpdateDTO = new PartnerCloudTableSaveOrUpdateDTO();
		saveOrUpdateDTO.setTable_id(tableId);
		List<String> updateFields = new ArrayList<>();
		updateFields.add(BuildingMessageEnum.BUILDING_ID.getFieldId());
		saveOrUpdateDTO.setUpdate_by_fields(updateFields);
		List<PartnerCloudTableItemDTO> items = new ArrayList<>();

		// 处理数据并封装
		if(Func.isNotEmpty(buildingMessageVOList)){
			for(BuildingMessageVO buildingMessageVO : buildingMessageVOList){
				PartnerCloudTableItemDTO item = new PartnerCloudTableItemDTO();
				Map<String,Object> fields = new HashMap<>(10);
				fields.put(BuildingMessageEnum.BUILDING_ID.getFieldId(), buildingMessageVO.getBuildingId());
				fields.put(BuildingMessageEnum.BUILDING_NAME.getFieldId(), buildingMessageVO.getBuildingName());
				fields.put(BuildingMessageEnum.USE_UNIT.getFieldId(), buildingMessageVO.getUserUnitName());
				fields.put(BuildingMessageEnum.CONTACT_PERSON.getFieldId(), buildingMessageVO.getContactsName());
				fields.put(BuildingMessageEnum.PHONE.getFieldId(), buildingMessageVO.getPhone());
				fields.put(BuildingMessageEnum.ADDRESS.getFieldId(), buildingMessageVO.getAddress());
				String type = buildingMessageVO.getBuildingType();
				if(!Func.isNull(type)){
					List<String> typeList = new ArrayList<>();
					String idByDictKey = BuildingMessageTypeEnum.getIdByDictKey(type);
					typeList.add(idByDictKey);
					fields.put(BuildingMessageEnum.TYPE.getFieldId(),typeList);
				}
				item.setFields(fields);
				items.add(item);
			}
		}
		saveOrUpdateDTO.setItems(items);
		partnerTableService.saveOrUpdateByFields(saveOrUpdateDTO);
	}
}
